package com.itheima.d3_stream;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 目标：Stream流的终结方法
 */
public class StreamTest4 {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        Student s1 = new Student("蜘蛛精", 26, 172.5);
        Student s2 = new Student("蜘蛛精", 26, 172.5);
        Student s3 = new Student("紫霞", 23, 167.6);
        Student s4 = new Student("白晶晶", 25, 169.0);
        Student s5 = new Student("牛魔王", 35, 183.3);
        Student s6 = new Student("牛夫人", 34, 168.5);
        Collections.addAll(students, s1, s2, s3, s4, s5, s6);

        //已知students集合存储了若干数据

        // 需求1：请计算出身高超过168的学生有几人。
        long count = students.stream()
                .filter(student -> student.getHeight() >= 168)
                .count();
        System.out.println(count);
        // 需求2：请找出身高最高的学生对象，并输出。
        Student student = students.stream()
                .max((stu1, stu2) -> Double.compare(stu1.getHeight(), stu2.getHeight()))
                .get();
        // 需求3：请找出身高最矮的学生对象，并输出。
        Student student1 = students.stream()
                .min((stu1, stu2) -> Double.compare(stu1.getHeight(), stu2.getHeight()))
                .get();
        // 需求4：请找出身高超过170的学生对象，并放到一个新集合中去返回。
        List<Student> list = students.stream()
                .filter(student2 -> student2.getHeight() > 170)
                .collect(Collectors.toList());
        System.out.println("list = " + list);
        // 需求5：请找出身高超过170的学生对象，并把学生对象的名字和身高，存入到一个Map集合返回。
        Map<String, Double> map = students.stream()
                .distinct()//去重复
                .filter(student3 -> student3.getHeight() > 170)
                .collect(Collectors.toMap(new Function<Student, String>() {
                    @Override
                    public String apply(Student student) {
                        return student.getName();
                    }
                }, new Function<Student, Double>() {
                    @Override
                    public Double apply(Student student) {
                        return student.getHeight();
                    }
                }));

        Map<String, Double> collect = students.stream()
                .filter(s -> s.getHeight() > 170)
                .distinct()
                .collect(Collectors.toMap(student2 -> student2.getName(), student2 -> student2.getHeight()));//
        System.out.println(map);
        System.out.println("----------------------------------");
        Object[] objects = students.stream()
                .distinct()
                .toArray();
        System.out.println(Arrays.toString(objects));
    }
}
